<?
class Controller {
    private static $CONTROLLER_KEY = "Controller";
	private $vars = array();
	private $messages = array();

	public function Controller() {
		$this->set("baseUrl", getBaseUrl());
		$this->set("title", getTitle());
	}

	public function index($vars) {
	}

	public function addHeader($head){
		$this->vars["header"][] = $head;
	}

	function set($key, $var) {
            if (isset($this->vars[$key]) == true) {
                    throw new Exception('Unable to set var `' . $key . '`. Already set.');
            }

            $this->vars[$key] = $var;
            return true;
    }
    
    function addMessage($msg){
    	$this->vars["messages"][] = $msg;
    }
    
    function addMessages($msgs){
    	foreach($msgs as $msg){
	    	$this->vars["messages"][] = $msg;
    	}
    }

    function setSession($var, $value){
    	$_SESSION[$var] = $value;
    }
    
    function getSession($var){
    	return $_SESSION[$var];
    }
    
    function isInSession($var){
    	return isset($_SESSION[$var]);
    }
    
	protected function redirect($view) {
		redirect($view);
	}

    // Renders a main using the specified template
    protected function renderText($text, $template = null) {
        if($template == null){
            $template = getTemplate();
        }
        $this->set('templateUrl', getBaseUrl().'/application/view/layout/');

        $templatePath = './application/view/layout/'. 'index.php';

        // Check to make sure the template file we're attempting to render exists
        if (file_exists($templatePath) == false) {
            trigger_error('Template `' . $template . '` does not exist.', E_USER_NOTICE);
            return false;
        }

        $staticText = $text;
        // And finally, include the actual main page
        include_once ($templatePath);
    }


    protected function renderTextWithoutTemplate($text){
        echo $text;
    }

    protected function renderViewWithoutTemplate($view){
        $viewPath = './application/view/' . $view . '.php';

        // Check to make sure the main file we're attempting to render exists
		if (file_exists($viewPath) == false) {
			trigger_error('View `' . $view . '` does not exist.', E_USER_NOTICE);
			return false;
		}

        foreach ($this->vars as $key => $value) {
            $$key = $value;
        }

        include_once $viewPath;
    }

	// Renders a main using the specified template
	protected function renderView($view, $template = null) {
        if($template == null){
            $template = getTemplate();
        }
		$this->set('templateUrl', getBaseUrl().'/application/view/layout/');

		//this should render the main in the main/controller/action folder
		$viewPath = './application/view/' . $view . '.php';
		$templatePath = './application/view/layout/'. 'index.php';

		// Check to make sure the main file we're attempting to render exists
		if (file_exists($viewPath) == false) {
			trigger_error('View `' . $view . '` does not exist.', E_USER_NOTICE);
			return false;
		}
 
		// Check to make sure the template file we're attempting to render exists
		if (file_exists($templatePath) == false) {
			trigger_error('Template `' . $template . '` does not exist.', E_USER_NOTICE);
			return false;
		}

        foreach ($this->vars as $key => $value) {
            $$key = $value;
        }

		// And finally, include the actual main page
		include_once ($templatePath);
	}

    public static function defaultRoute($controllerName){
        return strtolower(substr($controllerName, 0, strlen($controllerName) - strlen(Controller::$CONTROLLER_KEY)));
    }

    // Returns a list of all declared controllers
    public static function listControllers(){
        $controllers = array();
        foreach(get_declared_classes() as $class){
            if(endsWith($class, "Controller") && $class != Controller::$CONTROLLER_KEY){
                $controllers[] = $class;
            }
        }
        return $controllers;
    }
}
?>